package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.RcRegistrationContent;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.service.RcRegistrationContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * RcRegistrationContent的路由接口服务
 * 
 * @author 
 *
 */
@Slf4j
@RestController
@RequestMapping("RcRegistrationContentController")
@Api(tags = "居委会&志愿者活动管理", description = "")
public class RcRegistrationContentController {

	/** RcRegistrationContentService服务 */
	@Autowired
	private RcRegistrationContentService rcRegistrationContentService;
	
	/**
	 * 查询所有RcRegistrationContent数据的方法
	 * @return
	 */
	@GetMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
	public CommonResult find(Principal principal, @RequestParam(value = "title", required = false) String title,
                             @ApiParam(value = "1-app 2-小程序  3-公众号")@RequestParam(value = "channel", required = false) String channel,
                             @ApiParam(value = "审核状态 1-发布 2-审核中 3-审核不通过 4-审核通过 5-无效") @RequestParam(value = "status", required = false) String status,
                             @ApiParam(value = "查询类型 0-全部活动 1-我报名的 2-我做过的") @RequestParam(value = "queryType", required = false) String queryType,
                             @ApiParam(value = "是否被推荐  0-未推荐 1-已推荐") @RequestParam(value = "isRecommend", required = false) String isRecommend,
                             @ApiParam(value = "类型 01 - 志愿者 02-居委会办事指南") @RequestParam(value = "classified", required = false) String classified,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
        RcRegistrationContent build = RcRegistrationContent.builder().title(title).status(status)
                .channel(channel).userId(memberDetails.getUmsMember().getId() + "")
                .communityId(memberDetails.getUmsMember().getCommunityId()+"").isRecommend(isRecommend).classified(classified).build();
        return CommonResult.success(CommonPage.restPage(rcRegistrationContentService.find(build,queryType,pageNum,pageSize)));
	}
	
	/**
	 * 通过id查询RcRegistrationContent数据的方法
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/findOne", produces = {"application/json;charset=UTF-8"})
	public CommonResult findOne(@RequestParam(name="id") Integer id) {
		return CommonResult.success(rcRegistrationContentService.findOne(id));
	}
	
	/**
	 * 插入RcRegistrationContent属性不为空的数据方法
	 * @param value
	 * @return
	 */
	@PostMapping(value = "/save", produces = {"application/json;charset=UTF-8"})
	public CommonResult save(@RequestBody RcRegistrationContent value) {
        rcRegistrationContentService.saveNotNull(value);
		return CommonResult.success(null);
	}
	
	/**
	 * 更新RcRegistrationContent属性不为空的数据方法
	 * @param value
	 * @return
	 */
	@PostMapping(value = "/update", produces = {"application/json;charset=UTF-8"})
	public CommonResult update(@RequestBody RcRegistrationContent value) {
        rcRegistrationContentService.updateNotNullById(value);
		return CommonResult.success(null);
	}

	/**
	 * 通过id删除RcRegistrationContent数据方法
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/delete", produces = {"application/json;charset=UTF-8"})
	public CommonResult delete(@RequestParam(name="id") Integer id) {
        rcRegistrationContentService.deleteById(id);
        return CommonResult.success(null);
	}

	/**
	 * 通过id删除RcRegistrationContent数据方法
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/getStockTime", produces = {"application/json;charset=UTF-8"})
	public CommonResult getStockTime(@RequestParam(name="id") Integer id) {
        return CommonResult.success(rcRegistrationContentService.getStockTime(id));
	}





}
